package com.example.wanbaqian.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.WorkInfoDetailsBean;
import com.example.wanbaqian.customView.BottomAreaDialog;
import com.example.wanbaqian.utils.BaseUtils;
import com.example.wanbaqian.utils.CameraUtils;
import com.example.wanbaqian.utils.DialogUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.ImageCompressUtil;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

/**
 * 工作信息
 */
public class WorkInfoActivity extends BaseActivity implements OnAddressSelectedListener, BaseActivity.PermissionListeren, HttpCallback {
    private static final String TAG = "WorkInfoActivity";
    @BindView(R.id.work_info_company)
    EditText workInfoCompany;
    @BindView(R.id.work_info_telephone)
    EditText workInfoTelephone;
    @BindView(R.id.apply_one_position)
    TextView applyOnePosition;
    @BindView(R.id.work_info_address)
    EditText workInfoAddress;
    @BindView(R.id.work_info_time)
    TextView workInfoTime;
    @BindView(R.id.iv_one)
    ImageView ivOne;
    @BindView(R.id.work_photo_two)
    ImageView workPhotoTwo;
    @BindView(R.id.work_info_money)
    TextView workInfoMoney;
    private BottomAreaDialog dialog;
    private String imagePath;
    private boolean isDW = true;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case CommonValue.REQUEST_CODE1:
                    if (null != msg.obj) {
                        HttpUtils.getInstance().postFile(HttpURL.IMAGE_UPLOAD, mContext,
                                isDW ? CommonValue.company_door : CommonValue.job_station, new File(msg.obj.toString()),
                                isDW ? CommonValue.REQUEST_CODE1 : CommonValue.REQUEST_CODE2, WorkInfoActivity.this);
                        break;
                    } else {
                        showToast("图片上传失败，请重试!");
                    }
            }
        }
    };

    @Override
    protected int getLayout() {
        return R.layout.activity_work_info;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.colorBackground);
        setToolBar("工作信息");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.AUTH_JOB, mContext,
                new String[]{"accessid"}, new String[]{SPUtils.getAccessid(mContext)}), HttpURL.AUTH_JOB, CommonValue.REQUEST_CODE4, this);
        dialog = new BottomAreaDialog(mContext);
        dialog.setOnAddressSelectedListener(this);
        dialog.getCity();
    }

    @OnClick({R.id.work_info_money, R.id.apply_one_position, R.id.work_info_time, R.id.iv_one, R.id.work_photo_two, R.id.work_info_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.work_info_money:
                DialogUtils.showSingDialog("请选择月收入", "income", mContext, new DialogUtils.DialogInterface() {
                    @Override
                    public void backValue(String key, String value) {
                        workInfoMoney.setText(value);
                        workInfoMoney.setTag(key);
                    }
                });
                break;
            case R.id.apply_one_position:
                dialog.show();
                break;
            case R.id.work_info_time:
                DialogUtils.showSingDialog("请选择入职时间", "induction", mContext, new DialogUtils.DialogInterface() {
                    @Override
                    public void backValue(String key, String value) {
                        workInfoTime.setText(value);
                        workInfoTime.setTag(key);
                    }
                });
                break;
            case R.id.iv_one:
                requestPermission(CommonValue.REQUEST_CODE1, this, Manifest.permission.CAMERA);
                break;
            case R.id.work_photo_two:
                requestPermission(CommonValue.REQUEST_CODE2, this, Manifest.permission.CAMERA);
                break;
            case R.id.work_info_ok:
                String company = workInfoCompany.getText().toString();
                String telephone = workInfoTelephone.getText().toString();
                String address = workInfoAddress.getText().toString();
                if (TextUtils.isEmpty(company)) {
                    showToast("请填写公司名称");
                    return;
                } else if (TextUtils.isEmpty(telephone)) {
                    showToast("请填写公司电话号码");
                    return;
                } else if (TextUtils.isEmpty(address) || null == applyOnePosition.getTag()) {
                    showToast("请填写单位详细地址");
                    return;
                } else if (null == workInfoMoney.getTag()) {
                    showToast("请选择月收入");
                    return;
                } else if (null == workInfoTime.getTag()) {
                    showToast("请选择入职时间");
                    return;
                } else if (null == ivOne.getTag()) {
                    showToast("请上传单位门头照");
                    return;
                } else if (null == workPhotoTwo.getTag()) {
                    showToast("请上传你的工位牌");
                    return;
                } else {
                    HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.AUTH_ADDJOB, mContext
                            , new String[]{"accessid", "company_name", "company_phone", "company_area_no",
                                    "company_address", "induction_times", "month_income"}
                            , new String[]{SPUtils.getAccessid(mContext), company, telephone, applyOnePosition.getTag().toString()
                                    , address, workInfoTime.getTag().toString(), workInfoMoney.getTag().toString()})
                            , mContext, null, CommonValue.REQUEST_CODE3, this);
                }
                break;
        }
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        applyOnePosition.setText(province.name + city.name + county.name);
        applyOnePosition.setTag(county.id);
        dialog.dismiss();
    }

    @Override
    public void onGranted(int requestCode) {
        isDW = requestCode == CommonValue.REQUEST_CODE1 ? true : false;
        imagePath = CameraUtils.takePhoto(this);
    }

    @Override
    public void onDenied(int requestCode, List<String> deniedPermissions, List<String> noReminderPermissions) {
        showToast("请同意拍照权限");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i(TAG, "onActivityResult: " + imagePath);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CameraUtils.REQ_TAKE_PHOTO:
                    if (isDW) {
                        ivOne.setTag(null);
                        Glide.with(mContext).load(imagePath).into(ivOne);
                        ivOne.setTag("true");
                    } else {
                        workPhotoTwo.setTag(null);
                        Glide.with(mContext).load(imagePath).into(workPhotoTwo);
                        workPhotoTwo.setTag("true");
                    }
                    ImageCompressUtil.compressImage(mContext, imagePath, new ImageCompressUtil.ProcessImgCallBack() {
                        @Override
                        public void compressSuccess(String imgPath) {
                            Log.i(TAG, "compressSuccess: " + imgPath);
                            Message message = new Message();
                            message.obj = imgPath;
                            message.what = CommonValue.REQUEST_CODE1;
                            handler.sendMessage(message);
                        }
                    });
                    break;
            }
        }
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
            switch (requestCode) {
                case CommonValue.REQUEST_CODE1:
                        showToast("单位门头照上传成功");
                    break;
                case CommonValue.REQUEST_CODE2:
                        showToast("工位照上传成功");
                    break;
                case CommonValue.REQUEST_CODE3:
                    showToast("工作信息添加成功");
                    finish();
                    break;
                case CommonValue.REQUEST_CODE4:
                    WorkInfoDetailsBean workInfoDetailsBean = JsonUtils.getInstance().jsonToObjectk(content, WorkInfoDetailsBean.class);
                    if (null != workInfoDetailsBean) {
                        workInfoCompany.setText(BaseUtils.isNull(workInfoDetailsBean.getCompany_name()));
                        workInfoTelephone.setText(BaseUtils.isNull(workInfoDetailsBean.getCompany_phone()));
                        workInfoMoney.setText(BaseUtils.isNull(workInfoDetailsBean.getMonth_income_str()));
                        workInfoMoney.setTag(workInfoDetailsBean.getMonth_income());
                        applyOnePosition.setText(BaseUtils.isNull(workInfoDetailsBean.getCompany_area_no_str()));
                        applyOnePosition.setTag(workInfoDetailsBean.getCompany_area_no());
                        workInfoAddress.setText(BaseUtils.isNull(workInfoDetailsBean.getCompany_address()));
                        workInfoTime.setText(BaseUtils.isNull(workInfoDetailsBean.getInduction_times_str()));
                        workInfoTime.setTag(workInfoDetailsBean.getInduction_times());
                    }
                    break;
            }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {
    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }
}
